package com.service.scheduler.sched;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerKey;

import com.service.bean.device.Vehicle;
import com.service.cache.CacheManager;
import com.service.other.Logger;
import com.service.scheduler.job.DutyEndJob;
import com.service.scheduler.job.DutyStartJob;
import com.service.scheduler.server.SchedulerCreator;

/**
 * 上下班考勤总调度器
 * @author lj
 * Created on 2015年7月27日
 */
public class DutySched {

	private Scheduler sched;
	
	public void init() {
		try {
			sched = SchedulerCreator.createScheduler(DutySched.class.getSimpleName(), 20, Trigger.DEFAULT_PRIORITY);
			initDutySched();
			CacheManager.instance().schedMap.put(sched.getSchedulerName(),sched);
		} catch (Exception e) {
			Logger.err(e);
		}
	}
	
	private void initDutySched() throws SchedulerException {
		for (Vehicle vehicle : CacheManager.instance().vehicleMap.values()) {
			startSched(vehicle);
			endSched(vehicle);
		}
	}
	
	/**
	 * 生成车辆上班检查定时器
	 * @param vehicle
	 */
	private void startSched(Vehicle vehicle) {
		if (null == vehicle || null == vehicle.duty)
			return;
		try {
			Date date = null;
			if (0 == vehicle.duty.startFloatTime) {
				date = new Date(vehicle.duty.workStartTime.getTime() + 1 * 60 * 1000);
			} else {
				date = new Date(vehicle.duty.workStartTime.getTime() + vehicle.duty.startFloatTime * 60 * 1000);
			}
			String cronExp = SchedulerCreator.createCronExpr(date);
			JobKey jobKey = JobKey.jobKey("start_" + vehicle.device.deviceNo, "workstart");
			TriggerKey triggerKey = TriggerKey.triggerKey("start_" + vehicle.device.deviceNo, "workstart");
			JobDetail job = SchedulerCreator.createJob(jobKey, DutyStartJob.class);
			job.getJobDataMap().put("gpsno", vehicle.device.deviceNo);
			CronTrigger trigger = SchedulerCreator.createCronTriggerForJob(job, triggerKey, cronExp);
			sched.scheduleJob(job, trigger);
			Logger.debug(vehicle.device.deviceNo, 
					"车辆生成上班定时器, 定时器启动时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
		} catch (Exception e) {
			Logger.err(e);
		}
	}
	
	/**
	 * 生成车辆下班检查定时器
	 * @param vehicle
	 */
	private void endSched(Vehicle vehicle) {
		if (null == vehicle || null == vehicle.duty)
			return;
		try {
			Date date = null;
			if (0 == vehicle.duty.endFloatTime) {
				date = new Date(vehicle.duty.workEndTime.getTime() + 1 * 60 * 1000);
			} else {
				date = new Date(vehicle.duty.workEndTime.getTime() + vehicle.duty.endFloatTime * 60 * 1000);
			}
			String cronExp = SchedulerCreator.createCronExpr(date);
			JobKey jobKey = JobKey.jobKey("end_" + vehicle.device.deviceNo, "workend");
			TriggerKey triggerKey = TriggerKey.triggerKey("end_" + vehicle.device.deviceNo, "workend");
			JobDetail job = SchedulerCreator.createJob(jobKey, DutyEndJob.class);
			job.getJobDataMap().put("gpsno", vehicle.device.deviceNo);
			CronTrigger trigger = SchedulerCreator.createCronTriggerForJob(job, triggerKey, cronExp);
			sched.scheduleJob(job, trigger);
			Logger.debug(vehicle.device.deviceNo, "车辆生成下班定时器, 定时器启动时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
		} catch (Exception e) {
			Logger.err(e);
		}
	}
	
}
